package priv.hxm.jdbcUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import priv.hxm.myutils.Stringuutil;

public class MapUtil {
	private static Logger logger = Logger.getLogger(MapUtil.class);

	public static Object map2Object(Map<String, Object> map) {
		return null;
	}

	public static void main(String[] args) throws Exception {
		List<Map<String, Object>> findAll = JDBCUtil
				.findAll("select * from mkemp");
		Map<String, Object> map = findAll.get(0);
		Memp memp = map2Object(Memp.class, map);
	}

	/*
	 * ��mapת���ɶ��� except: List<Map<String, Object>> findAll = JDBCUtil
	 * .findAll("select * from mkemp"); Map<String, Object> map =
	 * findAll.get(0); Memp memp = map2Object(Memp.class, map);
	 */
	private static <T> T map2Object(Class<T> clzz, Map<String, Object> map)
			throws InstantiationException, IllegalAccessException,
			InvocationTargetException {

		T memp = clzz.newInstance();
		Method[] methods = Memp.class.getMethods();
		for (Method method : methods) {
			String name = method.getName();
			if (name.startsWith("set")) {
				String filed = Stringuutil.cape2underline(name.replaceFirst(
						"set", ""));
				Object object = map.get(filed.toUpperCase());
				method.invoke(memp, object);
			}
		}
		return memp;
	}
}
